<?php


namespace app\api\library\order\orderList;


use app\api\model\MallGoods;
use think\facade\Db;

class V
{
    public static function run($param)
    {
        $limit  = $param[ 'limit' ];
        $page   = $param[ 'page' ];
        $offset = ($page - 1) * $limit;
        $userId = _checkEmptyParam($param[ 'userId' ], '用户ID不能为空');
        $status = $param[ 'status' ];
        $where  = [
            ['user_id', '=', $userId],
        ];
        if ( !empty($status)) {
            $where[] = ['order_status', '=', $status];
        } else {
            $where[] = ['order_status', 'in', '30,50,60,70'];
        }
        $collageIds = Db::name('mall_order')
            ->where($where)
            ->column('collage_id');
        $collageIds = Db::name('mall_collage')
            ->where([['collage_id', 'in', $collageIds], ['finish_time', '>', 0]])
            ->column('collage_id');
        $where[]    = ['collage_id', 'in', $collageIds];
        $total      = Db::name('mall_order')
            ->where($where)
            ->count();
        $orders     = Db::name('mall_order')
            ->where($where)
            ->field('id,goods_id,goods_name,spec_id,spec_name,order_no,order_price,refund_status,order_status,create_time,receipt_time')
            ->limit($offset, $limit)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        $goodsIds   = array_column($orders, 'goods_id');
        $goods      = Db::name('mall_goods')->where([['id', 'in', $goodsIds]])
            ->field('id,title,logo,remark')
            ->select()->toArray();
        $goods      = array_column($goods, null, 'id');
        foreach ($orders as $key => $value) {
            $orders[ $key ][ 'goods' ]       = $goods[ $value[ 'goods_id' ] ] ?? [];
            $orders[ $key ][ 'order_price' ] = getPrice($value[ 'order_price' ]);
            $orders[ $key ][ 'create_time' ] = date('Y-m-d H:i:s', $value[ 'create_time' ]);
        }
        $page = [
            'total'      => $total,
            'limit'      => $limit,
            'page'       => $page,
            'total_page' => ceil($total / $limit),
        ];
        $res  = [
            'page' => $page,
            'list' => $orders,
        ];
        _outPut(200, $res);
    }
}